<template>
	<!-- 频道首页 -->
	<view class="app">
		<view class="nav-bar">
			<u-grid :col="4">
				<u-grid-item :custom-style="navItemStyle" v-for="(item, index) in navList" :key="index" @click="clickNav(index)">
					<image class="nav-icon" :src="item.icon" mode=""></image>
					<view class="nav-text">{{item.name}}</view>
				</u-grid-item>
			</u-grid>
		</view>
		<u-tabbar :list="vk.getVuex('$tabbar')"></u-tabbar>
	</view>
</template>

<script>
	import config from "@/app.config.js";
	let $GLOBAL = getApp().globalData;
	
	export default {
		data() {
			return {
				navList: getApp().globalData.navList, // 导航
				
				navItemStyle: {
					padding: '18rpx 16rpx 40rpx',
					color: "#999",
				},
			}
		},
		onLoad(options = {}) {
			// #ifdef H5
			this.$nextTick(() => {
				// #endif
				this.initPage(options);
				// #ifdef H5
			})
			// #endif
		},
		onReady() {

		},
		methods: {
			initPage() {
				// this.getNavList();
			},
			// 读取导航列表
			getNavList() {
				// this.navList = getApp().globalData.navList;
				this.navList = getApp().globalData.navList;
				if (config.debug) console.log('读取导航列表', this.navList)
			},
			// 点击了导航
			clickNav(index) {
				if (config.debug) console.log('点击了导航', index)
				
				let nav = this.navList[index];
				if (config.debug) console.log('导航', nav)
				
				if (!nav.href) {
					vk.toast('未维护导航地址');
					return;
				}
				// if(!this.$tools.checkLogin()) return
				
				if (nav.type == 'inner') {
					this.pageTo(nav.href);
				} else if (nav.type == 'wap') {
					this.pageToWap(nav.href);
				} else if (nav.type == 'wxmp') {
					this.pageToMP(nav.href);
				} else if (nav.type == 'func') {
					this.$tools[nav.href]()
				} else {
					vk.toast('导航尚未维护' + nav.type);
					return;
				}
			},
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 跳转到外部网站
			pageToWap(path) {
				// this.pageTo(`/pages/web_view/web_view?url=${path}`);
				location.href = path;
			},
			// 跳转到小程序
			pageToMP(appid) {
				// #ifdef MP-WEIXIN
				uni.navigateToMiniProgram({
					appId: appid,
					success(res) {
						// 打开成功
						console.log('打开成功')
					}
				})
				// #endif
				// #ifdef H5
				let appCode;
				if (appid == 'wxb85522c2bbe198bc') appCode = 'tijian';
				else if (appid == 'wx1bd7c68cb5318bf2') appCode = 'tiaojie';
				if (!appCode) {
					vk.toast('未配置小程序appid：' + appid);
					return;
				}
				this.$util.request(
						'http://api.haoyuekeji.top/http/router/plugs/lzy/weixin/client/pub.weixin.generateUrlLink', {
							appCode,
							path: '',
							query: '',
						}, 'GET')
					.then(res => {
						if (config.debug) console.log('获取小程序短链接res', res)
						if (res.code === 0) {
							location.href = res.data;
						} else {
							vk.toast(res.msg)
						}
					})
					.catch(err => {
						if (config.debug) console.log('获取小程序短链接err', err)
						vk.toast('获取小程序短链接err！' + JSON.stringify(err))
					})
				// #endif
			}
		},
		// 监听器
		watch: {

		},
		// 计算属性
		computed: {

		},
		onPullDownRefresh(){
			this.initPage();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
	}
</script>

<style lang="scss" scoped>
	.app{
		height: 100vh;
		background-color: #fff;
	}
</style>